package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.danmo.domain.*;
import com.muyu.danmo.mapper.DanmoGalleryDetailsMapper;
import com.muyu.danmo.service.IDanmoGalleryDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 画廊详情Service业务层处理
 *
 * @author yangle
 * @date 2024-10-23
 */
@Service
public class DanmoGalleryDetailsServiceImpl implements IDanmoGalleryDetailsService
{
    @Autowired
    private DanmoGalleryDetailsMapper danmoGalleryDetailsMapper;

    /**
     * 查询画廊详情
     *
     * @param id 画廊详情主键
     * @return 画廊详情
     */
    @Override
    public DanmoGalleryDetails selectDanmoGalleryDetailsById(Long id)
    {
        DanmoGalleryDetails danmoGalleryDetails = danmoGalleryDetailsMapper.selectDanmoGalleryDetailsById(id);
        danmoGalleryDetails.setViews(danmoGalleryDetails.getViews()+1);
         updateDanmoGalleryDetails(danmoGalleryDetails)  ;
        return danmoGalleryDetails;
    }

    /**
     * 查询画廊详情列表
     *
     * @param danmoGalleryDetails 画廊详情
     * @return 画廊详情
     */
    @Override
    public List<DanmoGalleryDetails> selectDanmoGalleryDetailsList(DanmoGalleryDetails danmoGalleryDetails)
    {
        return danmoGalleryDetailsMapper.selectDanmoGalleryDetailsList(danmoGalleryDetails);
    }

    /**
     * 新增画廊详情
     *
     * @param danmoGalleryDetails 画廊详情
     * @return 结果
     */
    @Override
    public Result<String> insertDanmoGalleryDetails(DanmoGalleryDetails danmoGalleryDetails)
    {

        try {
            danmoGalleryDetails.setCreateTime(DateUtils.getNowDate());
            danmoGalleryDetails.setStates(Long.valueOf(0));
            List<DanmoGalleryDetails> danmoGalleryDetails1 = danmoGalleryDetailsMapper.selectDanmoGalleryDetailsList(danmoGalleryDetails);
            if (danmoGalleryDetails1.size()>0)
            {
                return Result.error("您已发布过画廊，请勿重复发布");
            }
            danmoGalleryDetailsMapper.insertDanmoGalleryDetails(danmoGalleryDetails);

            Long id = danmoGalleryDetails.getId();
            danmoGalleryDetailsSetId(id, danmoGalleryDetails.getGalleryDoorList());
            danmoGalleryInteriorSetId(id, danmoGalleryDetails.getGalleryInteriorList());
            danmoGalleryWorksSetId(id, danmoGalleryDetails.getGalleryWorksList());

            addDanmoGalleryDetails( danmoGalleryDetails.getGalleryDoorList());
            addDanmoGalleryInteriorList(danmoGalleryDetails.getGalleryInteriorList());
            addDanmoGalleryWorksList(danmoGalleryDetails.getGalleryWorksList());


            // 处理画廊关系
            List<DanmoGalleryDetailsType> danmoGalleryDetailsTypes = new ArrayList<>();

            for (DanmoGalleryType danmoGalleryType : danmoGalleryDetails.getGalleryTypeList()) {
                DanmoGalleryDetailsType danmoGalleryDetailsType = new DanmoGalleryDetailsType();
                danmoGalleryDetailsType.setGalleryId(danmoGalleryDetails.getId());
                danmoGalleryDetailsType.setTypeId(danmoGalleryType.getId());
                danmoGalleryDetailsTypes.add(danmoGalleryDetailsType);
            }

            // 批量插入
            danmoGalleryDetailsMapper.batchInsert(danmoGalleryDetailsTypes);

            return Result.success("添加成功");
        }catch (Exception e){
            return Result.error("添加失败");
        }

    }

    private void addDanmoGalleryWorksList(List<DanmoGalleryWorks> galleryWorksList) {
        danmoGalleryDetailsMapper.addDanmoGalleryWorksList(galleryWorksList);
    }

    private void addDanmoGalleryInteriorList(List<DanmoGalleryInterior> galleryInteriorList) {
        danmoGalleryDetailsMapper.addDanmoGalleryInteriorList(galleryInteriorList);
    }

    private void addDanmoGalleryDetails(List<DanmoGalleryDoor> galleryDoorList) {
            danmoGalleryDetailsMapper.addDanmoGalleryDetails(galleryDoorList);
    }


    private void danmoGalleryWorksSetId(Long id, List<DanmoGalleryWorks> galleryWorksList) {
        for (DanmoGalleryWorks danmoGalleryWorks : galleryWorksList) {
            danmoGalleryWorks.setDetailsId(id);
        }
    }

    private void danmoGalleryInteriorSetId(Long id, List<DanmoGalleryInterior> galleryInteriorList) {
        for (DanmoGalleryInterior danmoGalleryInterior : galleryInteriorList) {
            danmoGalleryInterior.setDetailsId(id);
        }
    }

    private void danmoGalleryDetailsSetId(Long id, List<DanmoGalleryDoor> getGalleryDoorList) {
        for (DanmoGalleryDoor danmoGalleryDoor : getGalleryDoorList) {
            danmoGalleryDoor.setDetailsId(id);
        }
    }



    /**
     * 修改画廊详情
     *
     * @param danmoGalleryDetails 画廊详情
     * @return 结果
     */
    @Override
    public Result<String> updateDanmoGalleryDetails(DanmoGalleryDetails danmoGalleryDetails)
    {
        try{
        danmoGalleryDetails.setUpdateTime(DateUtils.getNowDate());
        danmoGalleryDetailsMapper.updateDanmoGalleryDetails(danmoGalleryDetails);

        Long id = danmoGalleryDetails.getId();

            deleteOldRelatedData(id);

        danmoGalleryDetailsSetId(id, danmoGalleryDetails.getGalleryDoorList());
        danmoGalleryInteriorSetId(id, danmoGalleryDetails.getGalleryInteriorList());
        danmoGalleryWorksSetId(id, danmoGalleryDetails.getGalleryWorksList());

        // 更新画廊门面列表
            addDanmoGalleryDetails( danmoGalleryDetails.getGalleryDoorList());
        // 更新画廊内部列表
            addDanmoGalleryInteriorList(danmoGalleryDetails.getGalleryInteriorList());
        // 更新画廊作品列表
            addDanmoGalleryWorksList(danmoGalleryDetails.getGalleryWorksList());
         // 更新画廊关系
         updateDanmoGalleryDetailsType(id, danmoGalleryDetails.getGalleryTypeList());


        return Result.success("更新成功");
    } catch (Exception e) {
    return Result.error("更新失败");
}
    }
    // 删除旧的门、作品和室内照片
    private void deleteOldRelatedData(Long id) {
        // 删除相关的门
        List<Long> oldDoorIds = danmoGalleryDetailsMapper.selectIdsByGalleryDoorIds(id);
        if (!oldDoorIds.isEmpty()) {
            danmoGalleryDetailsMapper.batchDeleteoldDoorIds(oldDoorIds);
        }

        // 删除相关的作品
        List<Long> oldWorkIds = danmoGalleryDetailsMapper.selectIdsByGalleryWorksIds(id);
        if (!oldWorkIds.isEmpty()) {
            danmoGalleryDetailsMapper.batchDeleteoldWorkIds(oldWorkIds);
        }

        // 删除相关的室内照片
        List<Long> oldInteriorIds = danmoGalleryDetailsMapper.selectIdsBytGalleryInterior(id);
        if (!oldInteriorIds.isEmpty()) {
            danmoGalleryDetailsMapper.batchDeleteOldtGalleryInteriorIds(oldInteriorIds);
        }
    }
    private void updateDanmoGalleryDetailsType(Long id, List<DanmoGalleryType> galleryTypeList) {
        // 删除旧的画廊关系
        danmoGalleryDetailsMapper.deleteDanmoGalleryDetailsType(id);
        // 添加新的画廊关系
        List<DanmoGalleryDetailsType> danmoGalleryDetailsTypes = new ArrayList<>();
        for (DanmoGalleryType danmoGalleryType : galleryTypeList) {
            DanmoGalleryDetailsType danmoGalleryDetailsType = new DanmoGalleryDetailsType();
            danmoGalleryDetailsType.setGalleryId(id);
            danmoGalleryDetailsType.setTypeId(danmoGalleryType.getId());
            danmoGalleryDetailsTypes.add(danmoGalleryDetailsType);
        }
        // 批量插入
        danmoGalleryDetailsMapper.batchInsert(danmoGalleryDetailsTypes);
    }
    /**
     * 批量删除画廊详情
     *
     * @param ids 需要删除的画廊详情主键
     * @return 结果
     */
    @Override
    public Result<String> deleteDanmoGalleryDetailsByIds(Long[] ids)
    {

        for (Long id : ids) {
            deleteOldRelatedData(id);
            danmoGalleryDetailsMapper.deledanmoGalleryDetailsType(id);
        }
        danmoGalleryDetailsMapper.deleteDanmoGalleryDetailsByIds(ids);
        return Result.success("删除成功");
    }

    /**
     * 删除画廊详情信息
     *
     * @param id 画廊详情主键
     * @return 结果
     */
    @Override
    public int deleteDanmoGalleryDetailsById(Long id)
    {
        return danmoGalleryDetailsMapper.deleteDanmoGalleryDetailsById(id);
    }
}
